<?php

declare(strict_types=1);

use App\ApplicationParams;
use Yiisoft\Html\Html;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\View\WebView;

/**
 * @var WebView $this
 * @var ApplicationParams $applicationParams
 * @var UrlGeneratorInterface $urlGenerator
 * @var list<array{id:int,token:string,label:?string,created_at:string,last_accessed_at:?string}> $webhooks
 * @var string|null $csrf
 */

$this->setTitle('Webhook Playground');
?>

<div class="page">
    <section class="hero">
        <h1>Webhook Playground</h1>
        <p>
            生成一个临时的 webhook URL，向它发送请求并实时查看完整的请求详情。
        </p>
        <form class="create-form" method="post" action="<?= Html::encode($urlGenerator->generate('webhook.create')) ?>">
            <?php if ($csrf !== null): ?>
                <?= $csrf->hiddenInput() ?>
            <?php endif; ?>
            <label>
                <span>可选标签</span>
                <input type="text" name="label" placeholder="例如：支付回调" maxlength="255">
            </label>
            <button type="submit">创建新的 Webhook</button>
        </form>
    </section>

    <section class="webhook-list">
        <div class="section-header">
            <h2>最近创建</h2>
            <p>选择一个 Webhook 查看请求日志与响应配置。</p>
        </div>
        <?php if ($webhooks === []): ?>
            <div class="empty-state">
                <p>还没有 webhook，点击上方按钮开始吧。</p>
            </div>
        <?php else: ?>
            <table>
                <thead>
                <tr>
                    <th>标签</th>
                    <th>令牌</th>
                    <th>创建于</th>
                    <th>最近访问</th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($webhooks as $webhook): ?>
                    <tr>
                        <td><?= Html::encode($webhook['label'] ?? '—') ?></td>
                        <td><code><?= Html::encode($webhook['token']) ?></code></td>
                        <td><time datetime="<?= Html::encode($webhook['created_at']) ?>"><?= Html::encode($webhook['created_at']) ?></time></td>
                        <td>
                            <?php if ($webhook['last_accessed_at'] !== null): ?>
                                <time datetime="<?= Html::encode($webhook['last_accessed_at']) ?>">
                                    <?= Html::encode($webhook['last_accessed_at']) ?>
                                </time>
                            <?php else: ?>
                                <span class="muted">未访问</span>
                            <?php endif; ?>
                        </td>
                        <td class="actions-cell">
                            <a class="primary-link" href="<?= Html::encode($urlGenerator->generate('webhook.show', ['token' => $webhook['token']])) ?>">查看</a>
                            <form class="inline-form" method="post" action="<?= Html::encode($urlGenerator->generate('webhook.delete', ['token' => $webhook['token']])) ?>">
                                <?php if ($csrf !== null): ?>
                                    <?= $csrf->hiddenInput() ?>
                                <?php endif; ?>
                                <button type="submit" class="button button-danger">删除</button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </section>
</div>
